package org.zk.dubbo_consumer.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Course;
import org.zk.entity.Lession;
import org.zk.entity.Teacher;
import org.zk.service.CatalogService;
import org.zk.service.CourseService;
import org.zk.service.LessionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 课节表 前端控制器
 * </p>
 *
 * @author 崔晨璞
 * @since 2019-12-11
 */
@Controller
@RequestMapping("/lession")
public class LessionController {

    @Resource
    private LessionService lessionService;
    @Resource
    private CatalogService catalogService;
    @Resource
    protected CourseService courseService;

    //课程详情页播放视频
    @RequestMapping("/getVideo")
    public ModelAndView getVideo(int lessionId, Course course) {
        List<Lession> catalog = catalogService.getCatalogByCid(course.getCourseId());
        Lession lession = lessionService.getLessionById(lessionId);
        ModelAndView modelAndView = new ModelAndView("user/play");
        modelAndView.addObject("catalog", catalog);
        modelAndView.addObject("lession", lession);
        return modelAndView;
    }


    //上传课程视频
    @RequestMapping("/addLession")
    public ModelAndView addLession(@RequestParam("file") MultipartFile file) {
        String videoName = file.getOriginalFilename();
        String suffix = videoName.substring(videoName.lastIndexOf("."));
        File fileMkdir = new File("E:\\courseVideo");
        if (!fileMkdir.exists()) {
            fileMkdir.mkdir();
        }
        String filePath = fileMkdir.getPath() + "\\";
        String newVideoName = UUID.randomUUID() + suffix;  //作为lession表的video存入
        File dest = new File(filePath + newVideoName);
        try {
            //写入文件
            file.transferTo(dest);
        } catch (IOException e) {
            return new ModelAndView("");
        }

        Lession lession = new Lession();
        int index = videoName.indexOf(".");
        videoName = videoName.substring(0, index);  //作为lession表的title
        lession.setLessionTitle(videoName);
        lession.setLessionVideo(newVideoName);
/*        Teacher teacher= (Teacher) session.getAttribute("teacher");
        int teacherId=teacher.getTeacherId();*/
        Course course = courseService.queryCourseIdByTid(1);
        lession.setCourse(course);
        lessionService.addLession(lession);
        return new ModelAndView("teacher/course-add");
    }

    public LessionService getLessionService() {
        return lessionService;
    }

    public void setLessionService(LessionService lessionService) {
        this.lessionService = lessionService;
    }

    public CatalogService getCatalogService() {
        return catalogService;
    }

    public void setCatalogService(CatalogService catalogService) {
        this.catalogService = catalogService;
    }

    public CourseService getCourseService() {
        return courseService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }
}

